from django.http import HttpResponse
from product.models import Product
from django.core.paginator import Paginator

def limit_query_view(request):
    # 限制数据返回
    # 1.切片操作,价格最高的前3条数据
    products =Product.objects.all().order_by('-price')[:3]
    
    
    # 2.从第2条取到第5条
    products = Product.objects.all().order_by('-price')[1:5]
    # print(products)
    
    # 3.分页
    products = Product.objects.all().order_by('price')
    paginator = Paginator(products,2)
    
    # 获取当前页码，page(1)表示获取第一页
    page_products = paginator.page(2)
    # print(page_products)
    
    # 获取总页数
    total_pages = paginator.num_pages
    # print(total_pages)
    
    # 获取当前页的数据
    for product in page_products:
        print(product)
    
    # 检查上一页
    if page_products.has_previous():
        previous_page = page_products.previous_page_number()
        print(f"当前页码：{page_products}，上一页：第{previous_page}页")
    else:
        print("没有上一页")
        
    # 检查下一页
    if page_products.has_next():
        next_page = page_products.next_page_number()
        print(next_page)
    else:
        print("没有下一页")
        
    return HttpResponse(f"{products}")
    
